<?php
$this->LoadHelper('ApiHelper');
$ApiHelper = new ApiHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('PublicHelper');
$PublicHelper = new PublicHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$check = $ApiHelper->tokenCheck($this->Param);
if(!$check['success']) ErrorMsg::ApiMsg($check['msg'], $check['code']);

//$test = file_get_contents('php://input');
//var_dump($test);print_r(json_decode($test));die();

if (empty($this->Param['option'])) {
    
} else {
    switch($this->Param['option']){
        case 'trosave': //初审单录入
//            wodrow\tools\Tools::log($_REQUEST);
            $save_type = empty($this->Param['savetype']) ? 'draft' : $this->Param['savetype'];
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            if($save_type != 'draft' && empty($this->Param['ot_contact']))                ErrorMsg::ApiMsg ('紧急联系人信息不能为空');
            //$ApiHelper->logSave('trosave', $this->Param); //临时调试
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
                        $data = array('oid' => $oid, 'uid' => $this->Param['uid']);
            $data['ct_spouse_detail'] = $this->Param['ct_spouse_detail'];
            $data['ct_guarantor_detail'] = $this->Param['ct_guarantor_detail'];
            $tab_owner = $PublicHelper->getTabFields('order_owner');
            foreach($_REQUEST as $k => $v){
                if(substr($k, 0, 3)=='ot_'&&in_array($k, $tab_owner)){
                    $data['owner'][$k] = $v;
                }
            }
            $tab_owner = $PublicHelper->getTabFields('order_car');
            foreach($_REQUEST as $k => $v){
                if(substr($k, 0, 3)=='ot_'&&in_array($k, $tab_owner)){
                    $data['car'][$k] = $v;
                }
            }
            $data['car']['ot_loan_limit'] = empty($data['car']['ot_loan_limit']) ? 0 : $data['car']['ot_loan_limit'];
            $data['baseorder'] = array(
                'o_buyer_type' => $this->Param['o_buyer_type'],
                'o_buyer_id' => $this->Param['o_buyer_id'],
                'o_buyer' => $this->Param['o_buyer'],
                'o_business_uid' => $this->Param['o_business_uid']
            );
            if(isset($data['owner']['ot_contact'])){
                $data['owner']['ot_contact'] = str_replace('\\', '', $data['owner']['ot_contact']);
                $data['owner']['ot_contact'] = json_decode($data['owner']['ot_contact'], true);
                foreach($data['owner']['ot_contact'] as $k => $v){
                    if(empty($v['name']) || empty($v['mobile']) || empty($v['relation'])) ErrorMsg::ApiMsg ('紧急联系人所有信息必填');
                }
                $data['owner']['ot_contact'] = serialize($data['owner']['ot_contact']);
            }
            if(isset($data['car']['ot_calculated'])){
                $data['car']['ot_calculated'] = str_replace('\\', '', $data['car']['ot_calculated']);
                $data['car']['ot_calculated'] = json_decode($data['car']['ot_calculated'], true);
                $data['car']['ot_bankrt_rate'] = $data['car']['ot_calculated']['bank_rate'] * 100;
                $data['car']['ot_calculated'] = serialize($data['car']['ot_calculated']);
            }
            $data['car']['ot_loan_limit'] = empty($data['car']['ot_loan_limit']) ? 0 : $data['car']['ot_loan_limit'];
            $data['vip']['ct_sign'] = $_REQUEST['oa_sign']?serialize(\wodrow\tools\ArrayHelp::str2arr($_REQUEST['oa_sign'])):null;
            $data['vip']['ct_video'] = $_REQUEST['oa_video']?serialize(\wodrow\tools\ArrayHelp::str2arr($_REQUEST['oa_video'])):null;
//            $data['vip']['oa_singlecard'] = $_REQUEST['oa_singlecard']?serialize(\wodrow\tools\ArrayHelp::str2arr($_REQUEST['oa_singlecard'])):null;
            $data['vip']['ct_issue'] = $_REQUEST['ct_issue']?$_REQUEST['ct_issue']:null;
            $data['vip']['ct_idaddress'] = $_REQUEST['ct_idaddress']?$_REQUEST['ct_idaddress']:null;
            $data['vip']['ct_validity_end'] = $_REQUEST['ct_validity_end']?$_REQUEST['ct_validity_end']:null;
            if ($data['vip']['ct_validity_end']=='0000-00-00'){
                $data['vip']['ct_validity_start'] == '1970-01-01';
            }
            $tab_owner = $PublicHelper->getTabFields('order_attachment');
            foreach($_REQUEST as $k => $v){
                if(substr($k, 0, 3)=='oa_'&&in_array($k, $tab_owner)){
                    $data['attachment'][$k] = $v;
                    if(in_array($k, array('oa_cert', 'oa_assets', 'oa_family', 'oa_flowing', 'oa_other','oa_contract', 'oa_singlecard'))){
                        if (isset($this->Param[$k])){
                            $data['attachment'][$k] = \wodrow\tools\ArrayHelp::str2arr($this->Param[$k]);
                        }
                    }
                    if (in_array($k, array('oa_sign', 'oa_video'))){
                        $data['attachment'][$k] = array();
                    }
                }
            }
            $order_row = $OrderHelper->orderRow($oid);
            if($order_row['o_status'] != '征信查询' && $save_type != 'draft'){
                ErrorMsg::ApiMsg('当前状态不允许处理');
            }
            //配偶详细信息转换
            // $ct_spouse = empty($data['ct_spouse_detail']) ? array() : $data['ct_spouse_detail'];
            $ct_spouse = empty($data['ct_spouse_detail']) ? array() : json_decode(str_replace('\\', '', $data['ct_spouse_detail']), TRUE);
            if(!empty($ct_spouse)){
                $data['cert']['spouse'][] = array(
                    'ct_detail' => $ct_spouse,
                    'ct_id' => $ct_spouse['ct_spouse_id']
                );
                if ($ct_spouse['ct_sign'])$data['attachment']['oa_sign'][] = \wodrow\tools\ArrayHelp::str2arr($ct_spouse['ct_sign']);
                if ($ct_spouse['ct_video'])$data['attachment']['oa_video'][] = \wodrow\tools\ArrayHelp::str2arr($ct_spouse['ct_video']);
            }
            $data['ct_spouse_detail'] = $ct_spouse?$ct_spouse:array();
            $ct_guarantor = empty($data['ct_guarantor_detail']) ? array() : json_decode(str_replace('\\', '', $data['ct_guarantor_detail']), TRUE);
            if(!empty($ct_guarantor)) foreach($ct_guarantor as $k => $v){
                $data['cert']['guarantor'][] = array(
                    'ct_detail' => $v,
                    'ct_id' => $v['ct_guarantor_id']
                );
                if ($v['ct_sign'])$data['attachment']['oa_sign'][] = \wodrow\tools\ArrayHelp::str2arr($v['ct_sign']);
                if ($v['ct_video'])$data['attachment']['oa_video'][] = \wodrow\tools\ArrayHelp::str2arr($v['ct_video']);
            }
            $data['ct_guarantor_detail'] = $ct_guarantor?$ct_guarantor:array();
            //根据贷款金额转换审核模式
            $mode_change = $BankHelper->modeChange($order_row['o_pre_mode'], $data['car']['ot_loan_limit']);
            if (isset($this->Param['oa_sign']))$data['attachment']['oa_sign'][] = \wodrow\tools\ArrayHelp::str2arr($this->Param['oa_sign']);
            if (isset($this->Param['oa_video']))$data['attachment']['oa_video'][] = \wodrow\tools\ArrayHelp::str2arr($this->Param['oa_video']);
            $t = array();
            foreach ($data['attachment']['oa_sign'] as $k => $v){
                $t = array_merge($t, $v);
            }
            $data['attachment']['oa_sign'] = $t;
            $t = array();
            foreach ($data['attachment']['oa_video'] as $k => $v){
                $t = array_merge($t, $v);
            }
            $data['attachment']['oa_video'] = $t;
            if($save_type == 'draft'){
                $data['attachment'] = $OrderHelper->attachmentIn($data['attachment'], $mode_change['mode_id'], false, false, false);
            }else{
                $data['attachment'] = $OrderHelper->attachmentIn($data['attachment'], $mode_change['mode_id'], false, false, false);
                if(!$data['attachment']){
                    //ErrorMsg::ApiMsg('所需面签资料上传不完全');
                }
            }
            if($save_type == 'draft'){
                $do = $OrderHelper->trialOrderSave($data);
            }else{
                //贷款利率校验
                $data['car']['o_bank_id'] = $order_row['o_bank_id'];
                $data['car']['oid'] = $oid;
                $rate_check = $BankHelper->rateCheck($data['car']);
                if(!$rate_check['success']){
                    ErrorMsg::ApiMsg($rate_check['msg']);
                }
                $data['baseorder']['o_pre_mode'] = $mode_change['mode_id'];
                $do = $OrderHelper->trialOrder($data);
            }
            if($do['success'] == true){
                $ApiHelper->logSave('trosave', $this->Param);
                ErrorMsg::ApiMsg('保存成功', 0);
            }else{                
                ErrorMsg::ApiMsg($do['msg']);
            }
            break;
        case 'trogt'://担保人补录
//            \wodrow\tools\Tools::log($_REQUEST);
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            //$ApiHelper->logSave('trogt', $this->Param); //临时调试
            $params = $this->Param;
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            $params['o_id'] = $oid;
            $result = $OrderHelper->guarantorUp($params);
            if($result['success']){
                $ApiHelper->logSave('trogt', $params);
                ErrorMsg::ApiMsg('保存成功', 0);
            }else{
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        case 'loanrate':
            $this->LoadHelper('BankHelper');
            $BankHelper = new BankHelper;
            $result = $BankHelper->rateCompute($this->Param);
            if($result['success']){
                ErrorMsg::ApiMsg('获取成功', 0, $result['data']);
            }else{
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        case 'exchange': //换配偶做主贷人
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作', 20000);
            $result = $OrderHelper->osExchange($oid);
            if($result['success']){
                ErrorMsg::ApiMsg('处理成功', 0);
            }else{
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        case 'getupatt': //根据贷款金额返回需要上传的附件
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            $ot_loan_limit = empty($this->Param['ot_loan_limit']) ? 0 : $this->Param['ot_loan_limit'];
            $order_row = $OrderHelper->orderRow($oid);
            $mode_attachment = $BankHelper->modeChange($order_row['o_pre_mode'], $ot_loan_limit);
            $re_attachment = array();
            if(!empty($mode_attachment['attachment'])){
                foreach($mode_attachment['attachment'] as $k => $v){
                    $re_attachment[$v] = $v;
                }
            }
            ErrorMsg::ApiMsg('获取成功', 0, $re_attachment);
            break;
        case 'guarantor_del': //删除担保人
            $ct_id = empty($this->Param['ct_id']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['ct_id'];
            $ct_row = $OrderHelper->certRow($ct_id);
            $order_check = $OrderHelper->checkOrderper($ct_row['ct_oid'], $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg ('无权操作');
            $do = $OrderHelper->certSave(array('ct_status' => 'del'), array('`ct_key` = ?' => 'guarantor', '`ct_id` = ?' => $ct_id));
            if($do){
                ErrorMsg::ApiMsg('删除成功', 0);
            }else{
                ErrorMsg::ApiMsg('删除失败');
            }
            break;
        case 'troremove': // 作废订单
            $oid = empty($this->Param['oid']) ? ErrorMsg::ApiMsg('参数错误') : $this->Param['oid'];
            $order_check = $OrderHelper->checkOrderper($oid, $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::ApiMsg('无权操作');
            $result = $OrderHelper->orderInvalid($oid);
            if($result['success']){
                $MemberHelper->saveLog($this->UserConfig, '作废客户', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::ApiMsg('处理成功', TRUE);
            }else{
                ErrorMsg::ApiMsg($result['msg']);
            }
            break;
        default :
            break;
    }
}